package com.vegas.netspider.memdb.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;

import com.vegas.netspider.dto.SportMatch;


public class QueryDao extends MemdbDao {

	@Override
	public void onClose() {
		
	}
	
	private List<SportMatchDao> daolist;
	
	private SportMatchDao getDao(String tb){
		if(daolist==null||daolist.isEmpty()||tb==null){
			return null;
		}
		
		for(SportMatchDao dao:daolist){
			if(dao.getTableName().equals(tb)){
				return dao;
			}
		}
		
		return null;
	}
	
	public List<SportMatch> query(String tb){
		SportMatchDao dao = this.getDao(tb);
		if(dao==null){
			return null;
		}
		
		return dao.query();
	}
	
	public List<SportMatch> query(String tb,int index,int size){
		SportMatchDao dao = this.getDao(tb);
		if(dao==null){
			return null;
		}
		
		return dao.query(index,size);
	}
	
	public List<SportMatch> query(String tb,List<Integer> ids){
		SportMatchDao dao = this.getDao(tb);
		if(dao==null){
			return null;
		}
		
		return dao.query(ids);
	}
	
	public List<SportMatch> query(String tb,String leagueNames){
		SportMatchDao dao = this.getDao(tb);
		if(dao==null){
			return null;
		}
		
		return dao.query(leagueNames);
	}
	
	public int totalSize(String tb){
		SportMatchDao dao = this.getDao(tb);
		if(dao==null){
			return 0;
		}
		
		return dao.getDao().queryForInt("select count(match_id) from "+dao.getTableName());
	}

	public List<String> listAllLeagueNames(String tb){
		
		SportMatchDao dao = this.getDao(tb);
		if(dao==null){
			return null;
		}
		
		return dao.getDao().query("select distinct league_name from "+dao.getTableName(),new RowMapper<String>(){
			@Override
			public String mapRow(ResultSet r, int arg1) throws SQLException {
				return r.getString(1);
			}
		});
	}

	public void setDaolist(List<SportMatchDao> daolist) {
		this.daolist = daolist;
	}
	
}
